const app = {
    state: {
        tabs: [],
    },
    mutations: {
        CHANGE_tabs: (state, { tabs }) => {
            state.tabs = tabs;
        }
    },
    actions: {
        handleChangetabs({ commit, state }, route) {
            const curtabs = route.matched[2]; // 当前路由页面
            const mobule = route.path.split('/')[1]; // 当前路由模块
            let tabs = state.tabs;

            // 判断是否在相同模块下切换路由
            let isSameMobule = state.tabs.every((item) => {
                const curMobule = item.path.split('/')[1];
                return curMobule === mobule;
            });

            // 判断是否已存在路由页
            let isHasTabs = state.tabs.some(item => item.path === route.path);
            if (isSameMobule) {
                if (isHasTabs) {
                    tabs = state.tabs;
                    console.log('the same mobule has:-----------' );
                    console.log(tabs);
                    console.log('the same mobule has:-----------' );
                } else {
                    tabs.push(curtabs)
                    console.log('the same mobule unhave:-----------' );
                    console.log(tabs);
                    console.log('the same mobule has:-----------' );
                }
            } else {
                tabs = [curtabs];
                console.log('not the same mobule:-----------' );
                console.log(tabs);
                console.log('not the same mobule:-----------' );
            }

            commit('CHANGE_tabs', { tabs });
        },
        handleRemoveTab({ commit, state }, tabName) {
            if (state.tabs.length === 1) {
                console.log('这是最后一个标签了');
                return;
            }
            const tabs = state.tabs.filter(item => item.meta.title !== tabName);
            commit('CHANGE_tabs', { tabs});
        }
    }
}

export default app;
